from datetime import datetime

from sqlalchemy import String, Boolean, DateTime, ForeignKey,  Text, Integer
from sqlalchemy.orm import Mapped, relationship, mapped_column
import sys
from pathlib import Path

from server.db.models.relationship_tables import user_roles, role_menus

sys.path.append(str(Path(__file__).parent.parent.parent))

from server.db.base import BaseModel




class User(BaseModel):
    """
    用户模型：  用户表
    """
    __tablename__ = 'user'
    username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False)
    real_name: Mapped[str] = mapped_column(String(50), nullable=True)
    password: Mapped[str] = mapped_column(String(255), nullable=False)
    phone: Mapped[str] = mapped_column(String(11), unique=True, nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
    last_login: Mapped[datetime] = mapped_column(DateTime, nullable=True)
    avatar: Mapped[str] = mapped_column(String(255), nullable=True)

    # 修正与role的关系
    roles: Mapped[list['Role']] = relationship(  # 改为复数形式
        "Role",
        secondary=lambda: user_roles,
        back_populates="users"  # 对应Role模型中的users
    )

    def __repr__(self):
        return f"<User {self.username}>"


class Role(BaseModel):
    """
    权限模型： 权限表
    """
    __tablename__ = 'role'

    name: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
    description: Mapped[str] = mapped_column(Text, nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    # 表关联
    # 修正与User的关系
    users: Mapped[list['User']] = relationship(  # 改为复数形式
        "User",
        secondary=lambda: user_roles,
        back_populates="roles"  # 对应User模型中的roles
    )

    # 修正与Menu的关系
    menus: Mapped[list['Menu']] = relationship(  # 改为复数形式
        "Menu",
        secondary=lambda: role_menus,
        back_populates="roles"  # 对应Menu模型中的roles
    )
    def __repr__(self):
        return f"<Role {self.name}>"


class Menu(BaseModel):
    """
    菜单模型： 菜单表
    """
    __tablename__ = 'menu'
    name: Mapped[str] = mapped_column(String(50), nullable=False)
    path: Mapped[str] = mapped_column(String(255), nullable=True)
    icon: Mapped[str] = mapped_column(String(50), nullable=True)
    order: Mapped[int] = mapped_column(Integer, default=0)
    parent_id: Mapped[str] = mapped_column(String(20), ForeignKey("menu.id"), nullable=True)
    is_show: Mapped[bool] = mapped_column(Boolean, default=True)
    permission: Mapped[str] = mapped_column(String(50))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
    parent: Mapped['Menu'] = relationship("Menu", remote_side="Menu.id", back_populates="children", foreign_keys=[parent_id])
    children: Mapped[list['Menu']] = relationship("Menu", back_populates="parent")

    # 修正与Permission的关系
    roles: Mapped[list['Role']] = relationship(  # 注意改为复数形式
        "Role",
        secondary=lambda: role_menus,
        back_populates="menus"  # 对应Role模型中的menus
    )

    def __repr__(self):
        return f"<Menu {self.name}>"